﻿@*
    表单 Permission
*@
@using XCode.Configuration;
@using XCode.Membership;
@using XCode;
@{
    Role role = null;
    if (Model.GetType().GetProperties().Length > 0)
    {
        role = ((System.Reflection.PropertyInfo[])Model.GetType().GetProperties())[0].GetValue(Model, null);
    }
    var menus = Menu.Root.AllChilds;

}

<script type="text/javascript">
    $(function () {
        $('input.authorize').on('change', function () {
            var $this = $(this);
            var status = $this.prop('checked');
            var childkey = $this.attr('child');
            // 规避change 需要在失去焦点时触发的问题，设置值完成后手工再次触发该事件
            $('input[parentkey="' + childkey + '"]').prop('checked', status).change();
        });
        // 只读/查看
        $('input.pro_detail').on('change', function () {
            $this = $(this);
            var status = $this.prop('checked');
            var key = $this.attr('prochildkey');
            $('input[proparentkey=' + key + ']').prop('checked', status);
        });
        // 全部权限
        $('input.pro_all').on('change', function () {
            $this = $(this);
            var status = $this.prop('checked');
            var key = $this.attr('prochildkey');
            $('input[prokey=' + key + ']').prop('checked', status);
        });
    });
</script>

<div class="accordion accordion-icon-toggle mt-5" id="kt_accordion_1">
    <div class="accordion-item">
        <div class="accordion-header d-flex justify-content-end pt-5 mb-5 me-5" data-bs-toggle="collapse" data-bs-target="#kt_accordion_permission">
            <h3 class="fs-4 fw-bolder mb-0 ms-4 text-primary">菜单操作授权</h3>
            <span class="accordion-icon ms-2"><span class="fas fa-arrow-right text-info"></span></span>
        </div>
        <div id="kt_accordion_permission" class="fs-6 collapse show" data-bs-parent="#kt_accordion_1">
            <div class="row table-responsive dataTables_wrapper dt-bootstrap4 m-0">
                <table class="table table-hover table-row-bordered table-row-dashed align-middle gy-4">
                    <thead class="fs-4">
                        <tr>
                            <th class="ps-5">名称</th>
                            <th>显示名</th>
                            <th>授权</th>
                            <th>操作权限</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var entity in menus)
                        {
                            <tr>
                                <td class="ps-5 w-lg-200px" style="max-width:360px;overflow:hidden;white-space: nowrap;text-overflow: ellipsis;" data-bs-toggle="tooltip" title="@entity.TreeNodeName">
                                    @if (entity is Menu menu)
                                    {
                                        if (!menu.Icon.IsNullOrEmpty())
                                        {
                                            <i class="fas @menu.Icon text-primary"></i>
                                        }
                                        else
                                        {
                                            <i class="fas fa-bookmark text-white"></i>
                                        }
                                    }
                                    @entity.TreeNodeName
                                </td>
                                <td class="w-lg-150px">@entity.DisplayName</td>
                                <td class="text-center w-lg-75px">
                                    <div class="form-check form-check-sm form-check-custom form-check-solid">
                                        @Html.CheckBox("p" + entity.ID, role.Has(entity.ID), new { @class = "authorize form-check-input", @child = "auth_child" + entity.ID, @parentkey = "auth_child" + entity.ParentID })
                                    </div>
                                </td>
                                <td>
                                    @if (entity.Childs.Count == 0)
                                    {
                                        <div class="d-flex align-items-center">
                                            @foreach (var item in entity.Permissions.OrderBy(e => e.Key))
                                            {
                                                var id = "pf" + entity.ID + "_" + ((Int32)item.Key);
                                                <div class="form-check form-check-sm form-check-custom form-check-solid me-5">
                                                    @Html.CheckBox(id, role.Has(entity.ID, (PermissionFlags)item.Key), new { @class = "form-check-input", @parentkey = "auth_child" + entity.ID, @proparentkey = "pro_" + item.Key + "_" + entity.ParentID, @prokey = "pro_" + (Int32)PermissionFlags.All + "_" + entity.ParentID })
                                                    @Html.Label(id, item.Value)
                                                </div>
                                            }
                                        </div>
                                    }
                                    else
                                    {
                                        <div class="d-flex align-items-center">
                                            <div class="form-check form-check-sm form-check-custom form-check-solid me-5">
                                                @Html.CheckBox("pc_readonly_" + entity.ID, false, new { @class = "form-check-input pro_detail", @prochildkey = "pro_" + (Int32)PermissionFlags.Detail + "_" + entity.ID })
                                                @Html.Label("pc_readonly_" + entity.ID, "只读")
                                            </div>

                                            <div class="form-check form-check-sm form-check-custom form-check-solid me-5">
                                                @Html.CheckBox("pc_all_" + entity.ID, false, new { @class = "form-check-input pro_all", @prochildkey = "pro_" + (Int32)PermissionFlags.All + "_" + entity.ID })
                                                @Html.Label("pc_all_" + entity.ID, "全部")
                                            </div>
                                        </div>

                                    }
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

    


